package md8800;

import gnu.io.PortInUseException;

import java.util.Vector;

import net.Network_iface;
import net.Network;

public class controller implements Network_iface
{
	private int _speed = 9600;
	private Network _network;
	private int _com_num = 2;

	public controller()
	{

	}

	public boolean connect() throws PortInUseException
	{
		_network = new net.Network(0, new net.Example(), 255);

		Vector<String> ports = _network.getPortList();

		boolean result = false;
		if (_network.connect(ports.elementAt(_com_num - 1), _speed))
		{
			result = true;
		}
		else
		{
			result = false;
		}
		return result;
	}

	@Override
	public void networkDisconnected(int id)
	{
		System.out.println("exit");
		_network.disconnect();

	}

	@Override
	public void parseInput(int id, int numBytes, int[] message)
	{
		System.out.print("received the following message: ");

		System.out.print(message[0]);
		for (int i = 1; i < numBytes; ++i)
		{
			System.out.print(", ");
			System.out.print(message[i]);
		}
		System.out.println();

	}

	@Override
	public void writeLog(int id, String text)
	{
		System.out.println("   log:  |" + text + "|");

	}

	public void sendString(String s)
	{
		_network.writeSerial(s);
	}

}
